﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.missions {
    import flash.events.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class MissionButtonMediator extends Mediator {

		[Inject]
        public var missions:MissionModel;
		[Inject]
        public var language:LanguageModel;
		[Inject]
        public var userModel:UserModel;
        private var _missionVO:MissionVO;

        override public function onRegister():void{
            super.onRegister();
            addContextListener(EventConstants.HIDE_MISSIONS, this.onHide);
            addContextListener(EventConstants.SHOW_MISSIONS, this.onShow);
            addContextListener(EventConstants.ON_SOLVED_MISSION, this.onSolved);
            addContextListener(EventConstants.ON_UPDATED_MISSION, this.onUpdated);
            addContextListener(EventConstants.ENABLE_MISSION_BUTTON, this.onEnabled);
            addViewListener(MouseEvent.CLICK, this.onClick);
            this.view.show();
            dispatch(new InfoEvent(EventConstants.INIT_MISSION));
            this.initButton();
        }
        private function get view():MissionButton{
            return ((viewComponent as MissionButton));
        }
        private function initButton():void{
            this._missionVO = this.missions.getMissionById(this.view.id);
            if (this._missionVO){
                this.view.icon = (this._missionVO.code + "icon");
            };
            this.view.solved = false;
            var _local1:String = this.language.getTextByKey("mission_general_label");
            var _local2:String = this.language.getTextByKey((("mission_" + this._missionVO.code) + "_title"));
            this.view.setTooltipData(((_local1 + " ") + _local2));
            if (this._missionVO.isNew){
                this.showMissionArrow("mission_new");
                this._missionVO.isNew = false;
            };
        }
        private function showMissionArrow(_arg1:String):void{
            var _local2:String = this.language.getTextByKey(_arg1);
            this.view.showArrow(_local2);
            this.view.enabled = true;
        }
        private function hideMissionArrow():void{
            this.view.hideArrow();
        }
        private function onClick(_arg1:MouseEvent):void{
            var _local3:InfoEvent;
            var _local4:Object;
            this.hideMissionArrow();
            var _local2:MissionVO = this._missionVO;
            if (_local2.isSolved){
                _local3 = new InfoEvent(EventConstants.COLLECT_MISSION_REWARD, _local2.id, _local2.code);
            } else {
                _local3 = new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.MISSION_TASKS_POPUP, _local2);
                _local4 = {type:"mission", st1:"view", st2:_local2.code};
                dispatch(new TrackingEvent(TrackingConst.TRACK, TrackingConst.EVENT, _local4));
            };
            dispatch(_local3);
        }
        private function onEnabled(_arg1:InfoEvent):void{
            var _local2:Boolean = Boolean(_arg1.info);
            this.view.enabled = _local2;
        }
        private function onHide(_arg1:Event):void{
            this.view.hide();
        }
        private function onShow(_arg1:Event):void{
            this.view.show();
        }
        private function onUpdated(_arg1:InfoEvent):void{
            if (((this._missionVO) && ((_arg1.id == this._missionVO.id)))){
                this.showMissionArrow("mission_progress");
            };
        }
        private function onSolved(_arg1:InfoEvent):void{
            if (((this._missionVO) && ((_arg1.id == this._missionVO.id)))){
                dispatch(new InfoEvent(EventConstants.COLLECT_MISSION_REWARD, this._missionVO.id, this._missionVO.code));
                dispatch(new InfoEvent(EventConstants.PLAY_EFFECT_SOUND, GameConstants.SOUND_SOLVE_MISSION));
            };
        }

    }
}//package net.wooga.pets.views.missions 
